package com.nageoffer.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nageoffer.shortlink.project.dao.entity.ShortLinkDO;
import com.nageoffer.shortlink.project.dao.mapper.ShortLinkMapper;
import com.nageoffer.shortlink.project.dto.req.MoveInRecycleBinReqDTO;
import com.nageoffer.shortlink.project.dto.req.RecoverRecycleBinReqDTO;
import com.nageoffer.shortlink.project.dto.req.RemoveRecycleBinReqDTO;
import com.nageoffer.shortlink.project.dto.req.ShortLinkRecycleBinPageReqDTO;
import com.nageoffer.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.nageoffer.shortlink.project.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.nageoffer.shortlink.project.common.constant.RedisKeyConstant.GOTO_IS_NULL_SHORT_LINK_KEY;
import static com.nageoffer.shortlink.project.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_KEY;

/**
 * 回收站实现层
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLinkDO> implements RecycleBinService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public void saveShortLinkToRecycleBin(MoveInRecycleBinReqDTO moveInRecycleBinReqDTO) {
        boolean update = lambdaUpdate()
                .eq(ShortLinkDO::getGid, moveInRecycleBinReqDTO.getGid())
                .eq(ShortLinkDO::getFullShortUrl, moveInRecycleBinReqDTO.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 0)
                .set(ShortLinkDO::getEnableStatus, 1)
                // 这里必须传参数，不然自动填充会失效
                .update(new ShortLinkDO());
        if (update) {
            stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY, moveInRecycleBinReqDTO.getFullShortUrl()));
        }
    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLink(ShortLinkRecycleBinPageReqDTO shortLinkRecycleBinPageReqDTO) {
        Page<ShortLinkDO> page = lambdaQuery()
                .in(ShortLinkDO::getGid, shortLinkRecycleBinPageReqDTO.getGids())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .orderByDesc(ShortLinkDO::getUpdateTime)
                .page(Page.of(shortLinkRecycleBinPageReqDTO.getCurrent(), shortLinkRecycleBinPageReqDTO.getSize()));
        return page.convert(each -> BeanUtil.toBean(each, ShortLinkPageRespDTO.class));
    }

    @Override
    public void recoverRecycleBin(RecoverRecycleBinReqDTO recoverRecycleBinReqDTO) {
        boolean update = lambdaUpdate()
                .eq(ShortLinkDO::getGid, recoverRecycleBinReqDTO.getGid())
                .eq(ShortLinkDO::getFullShortUrl, recoverRecycleBinReqDTO.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .set(ShortLinkDO::getEnableStatus, 0)
                // 这里必须传参数，不然自动填充会失效
                .update(new ShortLinkDO());
        if (update) {
            stringRedisTemplate.delete(String.format(GOTO_IS_NULL_SHORT_LINK_KEY, recoverRecycleBinReqDTO.getFullShortUrl()));
        }
    }

    @Override
    public void removeRecycleBin(RemoveRecycleBinReqDTO removeRecycleBinReqDTO) {
        lambdaUpdate()
                .eq(ShortLinkDO::getGid, removeRecycleBinReqDTO.getGid())
                .eq(ShortLinkDO::getFullShortUrl, removeRecycleBinReqDTO.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .eq(ShortLinkDO::getDelFlag,0)
                .remove();
    }
}
